package cn.succy.spider.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;
import java.util.List;

import cn.succy.spider.bean.Girl;

/**
 * 图片下载器
 * 
 * @Title DownLoader.java
 * @Description
 * @author Succy
 * @date 2016年11月14日 下午6:45:00
 * @version 1.0
 * @Company www.succy.cn
 */
public class DownLoader {
	private String filePath;
	private String dirPath;
	private Girl girl;
	private int threadCount = 1;// 线程数

	public DownLoader() {
	}

	/**
	 * 图片下载器参数初始化方法
	 * 
	 * @param girl
	 *            女孩对象
	 */
	public void init(Girl girl, String dirPath) {
		this.girl = girl;
		this.dirPath = dirPath;
		this.filePath = girl.getRealName();
	}

	public void download() {
		List<String> list = this.girl.getPics();
		// LinkedList<String> llist = new LinkedList<>(list);
		if (threadCount == 1) {
			singleThread(list);
		} else if (threadCount > 1) {
			// 为今后多线程做准备
			return;

		}
	}

	/**
	 * 
	 * @param url
	 * @param index
	 */
	private void download(String url, int index) {
		String subfix = url.substring(url.lastIndexOf("."));
		try {
			String dist = dirPath + "/" + filePath + "/";
			File file = new File(dist);
			if (!file.exists())
				file.mkdirs();
			Date date = new Date();

			String target = dist + date.getTime() + subfix;
			System.out.println(Thread.currentThread().getName() + ": 图片 [" + filePath
					+ date.getTime() + "] 开始下载");
			download("http:" + url, target);
			System.out.println(Thread.currentThread().getName() + ": 图片 [" + filePath
					+ date.getTime() + "] 下载完成");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 对指定的url进行下载
	 * 
	 * @param urlStr
	 *            图片的url
	 * @param filePath
	 *            图片存放的路径+图片的名字
	 * @throws IOException
	 */
	private void download(String urlStr, String tartet) throws IOException {
		InputStream in = null;
		URL url = new URL(urlStr);
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();

		try {
			in = conn.getInputStream();
		} catch (FileNotFoundException e) {
			return;
		}

		File file = new File(tartet);
		if (file.exists())
			return;
		FileOutputStream fout = new FileOutputStream(file);
		// 由于图片过大，进行压缩处理
		ImageCompress imc = new ImageCompress(in);
		imc.compress(fout, 1.5);

		fout.close();
		if (in != null)
			in.close();
	}

	/**
	 * 单线程时候下载
	 * 
	 * @param list
	 */
	private void singleThread(List<String> list) {
		for (int i = 0; i < list.size(); i++) {
			String url = list.get(i);
			download(url, i);
		}
	}

	public void setThreadCount(int threadCount) {
		this.threadCount = threadCount;
	}

	public int getThreadCount() {
		return threadCount;
	}

	public String getFilePath() {
		return filePath;
	}

	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}

	public String getDirPath() {
		return dirPath;
	}

	public void setDirPath(String dirPath) {
		this.dirPath = dirPath;
	}

	public Girl getGirl() {
		return girl;
	}

	public void setGirl(Girl girl) {
		this.girl = girl;
	}
}
